

package com.example.wtx.utils;

import com.example.wtx.Constants.CommonConstants;
import lombok.*;
import lombok.experimental.Accessors;

import java.io.Serializable;


/**
 * 响应信息主体
 * @param
 * @author lengleng, 曾建修改
 */
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class R implements Serializable {
    private static final long serialVersionUID = 1L;

    @Getter
    @Setter
    private int code;

    @Getter
    @Setter
    private String msg;

    @Getter
    @Setter
    private Object data;

    public static  R ok() {
        return restResult(null, CommonConstants.SUCCESS, null);
    }

    public static  R ok(String msg) {
        return restResult(null, CommonConstants.SUCCESS, msg);
    }

    public static  R ok(Object data) {
        return restResult(data, CommonConstants.SUCCESS, null);
    }

    public static  R ok(Object data, String msg) {
        return restResult(data, CommonConstants.SUCCESS, msg);
    }

    public static  R failed() {
        return restResult(null, CommonConstants.FAIL, null);
    }

    public static  R failed(String msg) {
        return restResult(null, CommonConstants.FAIL, msg);
    }

    public static  R failed(Object data) {
        return restResult(data, CommonConstants.FAIL, null);
    }

    public static  R failed(Object data, String msg) {
        return restResult(data, CommonConstants.FAIL, msg);
    }

    private static  R restResult(Object data, int code, String msg) {
        R apiResult = new R();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMsg(msg);
        return apiResult;
    }
}

